/**
 * 音乐，音效管理类
 */
import LocalStorageName from "./LocalStorageName";
import LocalCache from "./utils/LocalCache";

export default class AudioManager {
    //BGM ID
    public static _bgmAudioID: number = null;

    /**
     * 播放音效
     * @param clip 音效文件
     * @param loop 是否循环
     */
    public static playEffect(clip: cc.AudioClip, loop: boolean = false) {
        if (LocalCache.instance.getItem(LocalStorageName.EffectSwitch) === false) return;
        if (clip == null) return;
        let audioID = cc.audioEngine.play(clip, loop, 1);
        return audioID;
    }

    /**
     * 播放一次音效
     * @param clip 音效文件
     * @param loop 是否循环
     */
    public static playOnceEffect(clip: cc.AudioClip, loop: boolean = false): number {
        if (LocalCache.instance.getItem(LocalStorageName.EffectSwitch) === false) return;
        if (clip == null) return;
        let audioID = cc.audioEngine.play(clip, loop, 1);
        this.playOnceEffect = function () { return -1 };
        return audioID;
    }

    /**
     * 关闭音效
     * @param audioID 音效ID
     */
    public static stopEffect(audioID) {
        cc.audioEngine.stop(audioID);
    }

    /**
     * 播放BGM
     * @param clip BGM文件
     * @param loop 是否循环
     */
    public static playBGM(clip: cc.AudioClip, loop: boolean = true) {
        if (LocalCache.instance.getItem(LocalStorageName.BGMSwitch) === false) return;
        if (clip == null) return;
        this.stopBGM();
        AudioManager._bgmAudioID = cc.audioEngine.play(clip, loop, 1);
    }

    /**
     * 停止播放BGM
     */
    public static stopBGM() {
        if (AudioManager._bgmAudioID || AudioManager._bgmAudioID == 0) {
            // cc.audioEngine.stopMusic();
            cc.audioEngine.stop(AudioManager._bgmAudioID);
            AudioManager._bgmAudioID = null;
        }
    }

    /**
     * 暂停播放所有音频（包括BGM和音效）
     */
    public static pauseAll() {
        cc.audioEngine.pauseAll();
    }

    /**
     * 恢复之前所有音频（包括BGM和音效）
     */
    public static resumeAll() {
        cc.audioEngine.resumeAll();
    }

    /**
     * 设置音乐总开关（包含BGM和音效）
     * @param volumeSwitch 开？关？
     * @param clip BGM文件
     * @param loop 是否循环
     */
    public static setVolumeSwitch(volumeSwitch: boolean, clip: cc.AudioClip, loop: boolean = true) {
        if (volumeSwitch) {
            this.openEffect();
            this.openBGM(clip, loop);
        } else {
            this.closeEffect();
            this.closeBGM();
        }
    }

    /**
     * 关闭音效
     */
    public static closeEffect() {
        LocalCache.instance.setItem(LocalStorageName.EffectSwitch, false);
    }

    /**
     * 关闭BGM
     */
    public static closeBGM() {
        LocalCache.instance.setItem(LocalStorageName.BGMSwitch, false);
        this.stopBGM();
    }

    /**
     * 打开音效
     */
    public static openEffect() {
        LocalCache.instance.setItem(LocalStorageName.EffectSwitch, true);
    }

    /**
     * 打开BGM
     * @param clip BGM文件
     * @param loop 是否循环
     */
    public static openBGM(clip: cc.AudioClip, loop: boolean = false) {
        LocalCache.instance.setItem(LocalStorageName.BGMSwitch, true);
        this.playBGM(clip, loop);
    }
}
